package com.dalei.vds;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.DocumentException;

import com.dalei.vds.environment.CurrentTask;
import com.dalei.vds.model.Group;
import com.dalei.vds.model.Item;
import com.dalei.vds.widget.NewSimpleExpandableListAdapter;
import com.dalei.vds.xml.OutlineItemsXMLReader;

import android.app.TabActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class OutlineItemsActivity extends TabActivity {
	
	private static final String FIELD_ID = "id";

	private static final String FIELD_NAME = "name";

	private static final String FIELD_VALUE = "value";

	private static final String FIELD_TEXT = "text";

	private static final String FIELD_IMAGE = "image";
	
	private ExpandableListView outlineList;
	
	private ExpandableListView chassisDynamicList;
	
	private ExpandableListView chassisList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		TabHost tabHost = getTabHost();
		LayoutInflater.from(this).inflate(R.layout.outlineitems,
				tabHost.getTabContentView(), true);
		tabHost.setBackgroundColor(Color.TRANSPARENT);
		
		TabSpec tab1 = tabHost.newTabSpec(getString(R.string.tab_1));
		tab1.setIndicator(getString(R.string.vehicle_outline_detection));
		tab1.setContent(R.id.outlineitems_tab_outline);
		tabHost.addTab(tab1);

		TabSpec tab2 = tabHost.newTabSpec(getString(R.string.tab_2));
		tab2.setIndicator(getString(R.string.chassis_dynamic_detection));
		tab2.setContent(R.id.outlineitems_tab_chassis_dynamic);
		tabHost.addTab(tab2);

		TabSpec tab3 = tabHost.newTabSpec(getString(R.string.tab_3));
		tab3.setIndicator(getString(R.string.vehicle_chassis_detection));
		tab3.setContent(R.id.outlineitems_list_chassis);
		tabHost.addTab(tab3);

		TabSpec tab4 = tabHost.newTabSpec(getString(R.string.tab_4));
		tab4.setIndicator(getString(R.string.readme));
		tab4.setContent(R.id.outlineitems_tab_readme);
		tabHost.addTab(tab4);
		
		outlineList = (ExpandableListView) findViewById(R.id.outlineitems_list_outline);
		outlineList.setAdapter(new NewSimpleExpandableListAdapter(
				this, 
				getGroupData(0), 
				android.R.layout.simple_expandable_list_item_1, 
				new String[] { FIELD_NAME }, new int[] { android.R.id.text1 },
				getChildData(0), 
				R.layout.outline_list_item, 
				new String[] { FIELD_TEXT, FIELD_IMAGE },
				new int[] { R.id.outline_list_item_text, R.id.outlineitems_list_item_img }));
		outlineList.setOnChildClickListener(new IncomingOnChildClickListener());
		
		chassisDynamicList = (ExpandableListView) findViewById(R.id.outlineitems_list_chassis_dynamic);
		chassisDynamicList.setAdapter(new NewSimpleExpandableListAdapter(
				this, 
				getGroupData(1), 
				android.R.layout.simple_expandable_list_item_1, 
				new String[] { FIELD_NAME }, new int[] { android.R.id.text1 },
				getChildData(1), 
				R.layout.outline_list_item, 
				new String[] { FIELD_TEXT, FIELD_IMAGE },
				new int[] { R.id.outline_list_item_text, R.id.outlineitems_list_item_img }));
		chassisDynamicList.setOnChildClickListener(new IncomingOnChildClickListener());
		
		chassisList = (ExpandableListView) findViewById(R.id.outlineitems_list_chassis);
		chassisList.setAdapter(new NewSimpleExpandableListAdapter(
				this, 
				getGroupData(2), 
				android.R.layout.simple_expandable_list_item_1, 
				new String[] { FIELD_NAME }, new int[] { android.R.id.text1 },
				getChildData(2), 
				R.layout.outline_list_item, 
				new String[] { FIELD_TEXT, FIELD_IMAGE },
				new int[] { R.id.outline_list_item_text, R.id.outlineitems_list_item_img }));
		chassisList.setOnChildClickListener(new IncomingOnChildClickListener());
	}
	
	private List<Map<String, Object>> getGroupData(int tabId) {
		List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>();
		InputStream in = null;
		try {
			in = getAssets().open("outline.xml");
			OutlineItemsXMLReader reader = new OutlineItemsXMLReader(in);
			reader.read();
			List<Group> groups = reader.groups(tabId);
			if (groups != null) {
				for (Group group : groups) {
					Map<String, Object> data = new HashMap<String, Object>();
					data.put(FIELD_ID, group.getId());
					data.put(FIELD_NAME, group.getName());
					groupData.add(data);
				}
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return groupData;
	}

	private List<List<Map<String, Object>>> getChildData(int tabId) {
		List<List<Map<String, Object>>> childData = new ArrayList<List<Map<String, Object>>>();
		InputStream in = null;
		try {
			in = getAssets().open("outline.xml");
			OutlineItemsXMLReader reader = new OutlineItemsXMLReader(in);
			reader.read();
			List<Group> groups = reader.groups(tabId);
			if (groups != null) {
				for (Group group : groups) {
					List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
					List<Item> items = reader.items(tabId, group.getId());
					if (items != null) {
						for (Item item : items) {
							Map<String, Object> data = new HashMap<String, Object>();
							data.put(FIELD_ID, item.getId());
							data.put(FIELD_VALUE, item.getValue());
							data.put(FIELD_TEXT, item.getText());
							if (CurrentTask.getInvalidItems().contains(item.getValue())) {
								data.put(FIELD_IMAGE, R.drawable.not_passed);
							} else {
								data.put(FIELD_IMAGE, R.drawable.passed);
							}
							dataList.add(data);
						}
					}
					childData.add(dataList);
				}
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return childData;
	}
	
	private class IncomingOnChildClickListener implements OnChildClickListener {
		
		@SuppressWarnings("unchecked")
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			// TODO Auto-generated method stub
			ExpandableListAdapter adapter = (NewSimpleExpandableListAdapter) parent.getExpandableListAdapter();
			Object object = adapter.getChild(groupPosition, childPosition);
			if (object instanceof Map) {
				Map<String, Object> map = (Map<String, Object>) object;
				Object obj = map.get(FIELD_VALUE);
				if (obj instanceof Integer) {
					Integer value = (Integer) obj;
					if (CurrentTask.getInvalidItems().contains(value)) {
						CurrentTask.getInvalidItems().remove(value);
						ImageView img = (ImageView) v.findViewById(R.id.outlineitems_list_item_img);
						img.setImageResource(R.drawable.passed);
						map.put(FIELD_IMAGE, R.drawable.passed);
					} else {
						CurrentTask.getInvalidItems().add(value);
						ImageView img = (ImageView) v.findViewById(R.id.outlineitems_list_item_img);
						img.setImageResource(R.drawable.not_passed);
						map.put(FIELD_IMAGE, R.drawable.not_passed);
					}
				}
			}
			return false;
		}
		
	}
}